package com.taptech.doufu.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.sdk.universalimageloader.core.ImageLoader;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.db.history.Read_HirstoryUtil;
import com.taptech.doufu.util.DateUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class SectionNovelAdapter extends BaseListAdapter {
	private Context mContext;
	private String articleNum="";
	private String novelId="";
	private String novelTitle="";
	public List<HomeTopBean> mainList;
	private ArrayList<String> novelDetailsId;
	private LayoutInflater mLayoutInflater;
	protected ImageLoader imageLoader = null;
	private HomeTopBean bean;
	public HashMap<String, String >hirstoryList;

	public SectionNovelAdapter(Context mContext, String articleId) {
		this.mContext = mContext;
		novelId = articleId;
		Read_HirstoryUtil mRead_HirstoryUtil=new Read_HirstoryUtil(mContext);
		try {
			hirstoryList=mRead_HirstoryUtil.queryHirstory(novelId);
		}catch (Exception e){

		}

		mLayoutInflater = LayoutInflater.from(mContext);
		mainList = new ArrayList<HomeTopBean>();

	}

	public void setTitleAndNum(String articleNum, String title, ArrayList<String> novelDetailsId){
		this.articleNum = articleNum;
		novelTitle = title;
		this.novelDetailsId = novelDetailsId;
	}

	public void setHirstory(String sectionid){
		if (hirstoryList!=null) {
			hirstoryList.put(sectionid, sectionid);
		}
	}
	@Override
	public int getCount() {
		return mainList != null ? mainList.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return 0;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		NovelHolder novelHolder;
		if (convertView == null) {
			convertView=mLayoutInflater.inflate(R.layout.item_section_novel,null);
			novelHolder=new NovelHolder();
			novelHolder.novelTitle=(TextView)convertView.findViewById(R.id.item_section_novel_title);
			novelHolder.novelWordCount = (TextView) convertView.findViewById(R.id.item_section_novel_word_count);
			novelHolder.tvTime = (TextView) convertView.findViewById(R.id.item_section_novel_time);
			novelHolder.imgPay = (ImageView) convertView.findViewById(R.id.flag_2);
			convertView.setTag(novelHolder);
		} else {
			novelHolder=(NovelHolder)convertView.getTag();
		}
		bean=mainList.get(position);
		String id=bean.getId();
		novelHolder.novelWordCount.setText(bean.getLength() + "字");
		novelHolder.novelTitle.setText(bean.getTitle());//+"("+(position+1)+")");
		if(bean.getIs_pay() != null && "1".equals(bean.getIs_pay())){
			novelHolder.imgPay.setVisibility(View.VISIBLE);
		} else {
			novelHolder.imgPay.setVisibility(View.INVISIBLE);
		}
		if(bean.getUpdate_time() != null){
			novelHolder.tvTime.setText(DateUtil.dateToStrLong(new Date(Long.parseLong(bean.getUpdate_time()+"000"))));
		}
		if (hirstoryList!=null&&hirstoryList.containsKey(id)) {
			novelHolder.novelTitle.setTextColor(Color.parseColor("#bebebe"));
		}else{
			novelHolder.novelTitle.setTextColor(Color.parseColor("#666666"));
		}
		return convertView;
	}
	public void stopActivity(){
		((Activity)mContext).finish();
	}
	public synchronized void setListDate(List<HomeTopBean> mainList) {
		this.mainList.clear();
		this.mainList.addAll(mainList);
		notifyDataSetInvalidated();
	}

	public synchronized void addListDate(List<HomeTopBean> xx) {
		this.mainList.addAll(xx);
		notifyDataSetChanged();
	}

	public class NovelHolder extends ContentViewHolder {
		TextView novelTitle;
		TextView novelWordCount;
	}

	public void addHeaderView(View header) {

		if (header == null) {
			return;
		}

		this.mHeaderView = header;
		this.mHasHeaderView = true;

		notifyDataSetChanged();
	}

	public class ContentViewHolder {
		public HomeTopBean homeBean;
		public TextView commentCount;
		public TextView tvTime;
		public ImageView imgPay;
	}

	public HomeTopBean getBean(int postion) {
		HomeTopBean topBean = null;
		if (postion >= 0 && mainList != null && postion < mainList.size()) {
			topBean = this.mainList.get(postion);
		}
		return topBean;

	}
	public int getarticleNum() {
		return Integer.valueOf(articleNum);

	}

	public String getNovelId() {
		return novelId;

	}
	public String getNovelTitle() {
		return novelTitle;

	}

	public ArrayList<String> getNovelDetailsId() {
		return novelDetailsId;
	}

}
